package com.example.springbott.wx_service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.common.BasePageInfo;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.kerbal.GoodsDetailVo;
import com.example.springbott.bean.kerbal.SpecCommonDate;
import com.example.springbott.bean.kerbal.WxGoodsCategoryVo;
import com.example.springbott.bean.silic.MarketCategoryVo;
import com.example.springbott.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.example.springbott.util.Count.ROOT_CATEGORY_PID;

@Component
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketIssueMapper marketIssueMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public Integer wxHomeCount() {
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(new MarketGoodsExample());
        return marketGoods.size();
    }


    @Override
    public WxGoodsCategoryVo wxGoodsCategory(Integer id) {
        //
        MarketCategory judge = marketCategoryMapper.selectByPrimaryKey(id);
        WxGoodsCategoryVo wxGoodsCategoryVo = new WxGoodsCategoryVo();
        int code = judge.getPid().intValue();

        if (code == 0) {
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andPidEqualTo(id);
            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
            wxGoodsCategoryVo.setBrotherCategory(marketCategories);

            if (marketCategories != null && marketCategories.size() > 0) {
                MarketCategory marketCategory = marketCategories.get(0);
                wxGoodsCategoryVo.setCurrentCategory(marketCategory);
            }
            MarketCategoryVo marketCategoryVo = marketCategoryMapper.selectCategoryById(id);
            wxGoodsCategoryVo.setParentCategory(marketCategoryVo);
            return wxGoodsCategoryVo;
        } else {
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andPidEqualTo(code);
            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
            wxGoodsCategoryVo.setBrotherCategory(marketCategories);
            wxGoodsCategoryVo.setCurrentCategory(judge);
            MarketCategoryVo marketCategoryVo = marketCategoryMapper.selectCategoryById(id);
            wxGoodsCategoryVo.setParentCategory(marketCategoryVo);
            return wxGoodsCategoryVo;
        }
    }


    @Override
    public SpecCommonDate WxGoodsList(BasePageInfo basePageInfo, Integer categoryId, String keyword, Integer brandId) {
        SpecCommonDate<Object> objectSpecCommonDate = new SpecCommonDate<>();
        //filterList部分
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidNotEqualTo(ROOT_CATEGORY_PID);
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(new MarketCategoryExample());

        //goodsList部分
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = marketGoodsExample.createCriteria();

        if (categoryId != null && categoryId != 0) {
            goodsExampleCriteria.andCategoryIdEqualTo(categoryId);
        }
        if (keyword != null) {
            goodsExampleCriteria.andNameLike("%" + keyword + "%");
        }
        if (brandId != null) {
            goodsExampleCriteria.andBrandIdEqualTo(brandId);
        }
        goodsExampleCriteria.andDeletedEqualTo(false);
        //分页部分
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> marketGoodsPageInfo = new PageInfo<>(marketGoods);

        SpecCommonDate data = SpecCommonDate.data(marketGoodsPageInfo);
        data.setFilterCategoryList(marketCategories);
        return data;
    }


    @Override
    public GoodsDetailVo wxGoodsDetail(Integer id) {

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        Integer brandId = marketGoods.getBrandId();
        //Attribute
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria marketGoodsAttributeExampleCriteria = marketGoodsAttributeExample.createCriteria();
        marketGoodsAttributeExampleCriteria.andDeletedEqualTo(false);
        marketGoodsAttributeExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
        goodsDetailVo.setAttribute(marketGoodsAttributes);
        //brand
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(brandId);
        if (marketBrand != null) {
            goodsDetailVo.setBrand(marketBrand);
        } else {
            goodsDetailVo.setBrand(new MarketBrand());
        }
        //comment
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentExampleCriteria = marketCommentExample.createCriteria();
        commentExampleCriteria.andDeletedEqualTo(false);
        commentExampleCriteria.andValueIdEqualTo(id);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        GoodsDetailVo.CommentDTO comment = new GoodsDetailVo.CommentDTO();
        for (int i = 0; i < marketComments.size(); i++) {
            MarketComment marketComment = marketComments.get(i);
            marketComment.setPicList(marketComment.getPicUrls());
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketComment.getUserId());
            marketComment.setNickname(marketUser.getNickname());
        }
        comment.setData(marketComments);
        comment.setCount(marketComments.size());
        goodsDetailVo.setComment(comment);

        //info

        goodsDetailVo.setInfo(marketGoods);
        //issue
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(new MarketIssueExample());
        goodsDetailVo.setIssue(marketIssues);
        //productList
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria productExampleCriteria = marketGoodsProductExample.createCriteria();
        productExampleCriteria.andDeletedEqualTo(false);
        productExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
        goodsDetailVo.setProductList(marketGoodsProducts);
        //
        goodsDetailVo.setShare(true);
        goodsDetailVo.setShareImage("");
        //specificationList
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria specificationExampleCriteria = marketGoodsSpecificationExample.createCriteria();
        specificationExampleCriteria.andDeletedEqualTo(false);
        specificationExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);

        List<GoodsDetailVo.SpecificationListDTO> specificationListDTOS = new ArrayList<>();
        GoodsDetailVo.SpecificationListDTO specificationListDTO = new GoodsDetailVo.SpecificationListDTO();
        specificationListDTO.setValueList(marketGoodsSpecifications);

        if (marketGoodsSpecifications != null) {
            specificationListDTO.setName(marketGoodsSpecifications.get(0).getSpecification());
        }
        specificationListDTOS.add(specificationListDTO);
        goodsDetailVo.setSpecificationList(specificationListDTOS);
        //go
        goodsDetailVo.setGroupon(new LinkedList<>());
        //wxColletction:如果客户登录，就进行判断，如果没有登录，默认无法使用
        //可以进行优化，使用session
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal == null) {
            goodsDetailVo.setUserHasCollect(0);
        } else {
            MarketCollectExample marketCollectExample = new MarketCollectExample();
            MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
            criteria.andUserIdEqualTo(principal.getId());
            criteria.andDeletedEqualTo(false);
            criteria.andValueIdEqualTo(id);
            if (marketCollectMapper.selectByExample(marketCollectExample).size() > 0) {
                goodsDetailVo.setUserHasCollect(1);
            }

        }
        return goodsDetailVo;


    }


    @Override
    public CommonData wxGoodsRelated(Integer id) {
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        Integer categoryId = marketGoods.getCategoryId();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = marketGoodsExample.createCriteria();
        goodsExampleCriteria.andDeletedEqualTo(false);
        goodsExampleCriteria.andCategoryIdEqualTo(categoryId);
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> marketGoodsPageInfo = new PageInfo<>(marketGoodsList);
        return CommonData.data(marketGoodsPageInfo);


    }

    /**
     * 点击浏览商品，留足迹，向数据库插入足迹 detail接口
     *
     * @param /goodsId
     * @return /null
     * @author silic xia
     * @since 2022/11/24 11:42
     */
    @Override
    public void wxGoodsDetailFootprint(Integer goodsId) {
        MarketFootprint marketFootprint = new MarketFootprint();
        marketFootprint.setAddTime(new Date());
        marketFootprint.setUpdateTime(new Date());
        marketFootprint.setGoodsId(goodsId);
        marketFootprint.setDeleted(false);
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        marketFootprint.setUserId(user.getId());
        footprintMapper.insertSelective(marketFootprint);
    }
}
